﻿using System;
using UnityEditor;
using UnityEngine;
using UnityEngine.UIElements;
using static winS.UnityEditor.ProcessEditor.ProcessEditorData;

namespace winS.UnityEditor.ProcessEditor
{
    /// <summary>
    /// 用ImmediateModeElement绘制的背后网格背景
    /// </summary>
    public class GridBackground : ImmediateModeElement
    {
        private float lineWidth => backgroundData.lineWidth;
        private float minInterval => backgroundData.minInterval;
        private float maxInterval => backgroundData.maxInterval;

        private int thickLineInterval => backgroundData.rulerlineInterval;

        private Color lineColor => backgroundData.lineColor;
        private Color thickLineColor => backgroundData.rulerlineColor;
        private Color backgroundColor => backgroundData.backgroundColor;

        private readonly BackgroundData backgroundData;

        public GridBackground(BackgroundData backgroundData)
        {
            name = "网格背景";
            this.backgroundData = backgroundData;
        }

        protected override void ImmediateRepaint()
        {
            if (parent is not StepGraphView stepGraphView) throw new Exception($"{nameof(GridBackground)} 只能附加在 {nameof(StepGraphView)} 下");

            //VisualElement contentElement = stepGraphView;
            VisualElement contentElement = stepGraphView.contentViewContainer;

            Rect layout = contentElement.panel.visualTree.layout;
            //Rect layout = stepGraphView.panel.visualTree.layout;
            EditorGUI.DrawRect(layout, backgroundColor);//绘制背景

            float maxScale = stepGraphView.maxScale;
            float minScale = stepGraphView.minScale;

            float scaleRange = maxScale - minScale;
            float rate = (stepGraphView.currentScale - minScale) / scaleRange;
            float currentInterval = minInterval + (maxInterval - minInterval) * rate;

            Vector2 currentOffset = contentElement.transform.position;//左正 右负,上负,下正
            float width = layout.width;
            float height = layout.height;

            Rect xLineRect = layout;
            xLineRect.width = lineWidth;
            xLineRect.x += currentOffset.x % currentInterval;
            int countX = thickLineInterval - (int)(currentOffset.x / currentInterval) % thickLineInterval;//计算在当前偏移下,已经绘制了多少条水平线

            Rect yLineRect = layout;
            yLineRect.height = lineWidth;
            yLineRect.y += currentOffset.y % currentInterval;
            int countY = thickLineInterval - (int)(currentOffset.y / currentInterval) % thickLineInterval;

            float currentWidth = 0f;
            float currentHeight = 0f;

        ContinueDraw:
            bool isDrawX = currentWidth < width;
            bool isDrawY = currentHeight < height;
            if (!isDrawX && !isDrawY) return;
            if (isDrawX)
            {
                Color color = countX % thickLineInterval == 0 ? thickLineColor : lineColor;
                EditorGUI.DrawRect(xLineRect, color);
                currentWidth += currentInterval;
                xLineRect.x += currentInterval;
                countX++;
            }
            if (isDrawY)
            {
                Color color = countY % thickLineInterval == 0 ? thickLineColor : lineColor;
                EditorGUI.DrawRect(yLineRect, color);
                currentHeight += currentInterval;
                yLineRect.y += currentInterval;
                countY++;
            }
            goto ContinueDraw;
        }
    }
}